package com.caac.core.util;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;

/**
 * <p>Title: Intolong</p>
 * <p>Description: Intolong</p>
 * <p>Copyright: Copyright (c) 2004 Intolong</p>
 * <p>Company: Intolong</p>
 * @author <a href="mailto:intolong@163.com">Intolong</a>
 * @version 1.0
 */

public final class ParameterHandler {
    private Map map;

    public ParameterHandler(HttpServletRequest request) {
        map = new HashMap();
        int xml=1;
        int ssh=1;
        int sms=1;
        int jyj=1;
       
        for (Enumeration names = request.getParameterNames(); names
                .hasMoreElements();) {
            String name = (String) names.nextElement();
            if("xmlxFlag".equals(name)){
            	xml=0;
            }
            if("sshyFlag".equals(name)){
            	ssh=0;
            }
            if("xmszdFlag".equals(name)){
            	sms=0;
            }
            if("jyjgFlag".equals(name)){
            	jyj=0;
            }
        }
        for (Enumeration names = request.getParameterNames(); names
        .hasMoreElements();) {
		    String name = (String) names.nextElement();
		    String[] values = request.getParameterValues(name);
		    if("xmlxs".equals(name)){
            	if(xml==0){
            		continue;
            	}
            }
		    if("sshys".equals(name)){
            	if(ssh==0){
            		continue;
            	}
            }
		    if("xmszd".equals(name)){
            	if(sms==0){
            		continue;
            	}
            }
		    if("jyjgs".equals(name)){
            	if(jyj==0){
            		continue;
            	}
            }
		    if (values != null && values.length != 0) {
		        List list = null;
		        for (int i = 0; i < values.length; i++) {
		            if (values[i] != null && values[i].length() != 0) {
		                if (list == null) {
		                    list = new ArrayList();
		                }
		                list.add(values[i]);
		            }
		        }
		        if (list != null) {
		            map.put(name, list);
		        }
		    }
		}
    }

    public void append(String name, String value) {
        if (map.containsKey(name)) {
            List list = (List) map.get(name);
            if (!list.contains(value)) {
                list.add(value);
            }
        } else {
            add(name, value);
        }
    }

    public void add(String name, String value) {
        List list = new ArrayList();
        list.add(value);
        map.put(name, list);
    }

    public void remove(String name) {
        map.remove(name);
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();
        for (Iterator it = map.keySet().iterator(); it.hasNext();) {
            String name = (String) it.next();
            List list = (List) map.get(name);
            for (int i = 0; i < list.size(); i++) {
                String param = null;
                param = (String) list.get(i);
                try {
                   //param = URLEncoder.encode( (String) list.get(i), "UTF-8");
                } catch (Exception e) {
                   param = (String) list.get(i);
                }               
                 
                if (!"first".equals(name)) {
                    sb.append(name).append("=").append(param).append("&");
                }
            }
        }
        if (sb.indexOf("&") != -1) {
            sb.deleteCharAt(sb.length() - 1);
        }
        return sb.toString();
    }
}
